(defun trianglep(a b c)
  (= (+ (expt a 2) (expt b 2)) (expt c 2)))

(defun sides (p)
  (loop for i from 1 to (1- (floor (/ p 2)))
     with sols = 0
     do (loop for j from i to (1- (floor (/ p 2)))
	   for k = (- p (+ i j))
	   when (and (> k j i) (trianglep i j k))
	   ;do (setq sols (cons (list i j k) sols)))
	   do (setq sols (1+ sols))
	   ;do (format t "~d ~d ~d ~%" i j k))
     finally (return sols))))

(defun no-of-triangles (p)
  (loop for a from 1 to (1- (floor (/ p 2))) 
     with sol = 0
     do (loop for b = a then (1+ b)
	   for c = (- p (+ a b))
	   until (> b c)
	   when (and (<= a b c) (trianglep a b c))
	   do (setq sol (incf sol)));(cons (list a b c) sol)))
     finally (return sol)))
       
(defun probl39 ()
  (loop for p to 999;= 3 then (+ 2 p)
     ;until when (>= p 999)
     with max = p
     with maxp = p
     for triangles = (no-of-triangles p)
     when (> triangles max) 
     do (setq max triangles maxp p)
     finally (return maxp)))